package com.quanshu.exchange.ui.gui.message.friends;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baselibrary.util.glide.GlideUtils;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.db.UserDao;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.widget.AutoScaleImageButton;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 搜索好友 - 详细资料
 * Created by jye on 2017/12/13.
 */

public class UserDetailActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.userDetailAvatar)
    ImageView userDetailAvatar;
    @BindView(R.id.userDetailMemberId)
    TextView userDetailMemberId;
    @BindView(R.id.userDetailNickname)
    TextView userDetailNickname;
    @BindView(R.id.userDetailTel)
    TextView userDetailTel;
    @BindView(R.id.userDetailBtn)
    AutoScaleImageButton userDetailBtn;
    @BindView(R.id.userDeleteBtn)
    Button userDeleteBtn;

    private UserEntity userEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_detail);
        ButterKnife.bind(this);
        initIntent();
        initView();
        initListener();
    }

    private void initIntent() {
        userEntity = (UserEntity) getIntent().getSerializableExtra("UserEntity");
    }

    private void initView() {
        if (userEntity != null) {
            // 是好友
            if (userEntity.getIs_friend() == 1) {
                userDetailBtn.setVisibility(View.GONE);
                userDeleteBtn.setVisibility(View.VISIBLE);
            } else if (userEntity.getMemberid().equals(currentUser.getMemberid())) {
                userDetailBtn.setVisibility(View.GONE);
                userDeleteBtn.setVisibility(View.GONE);
            } else {
                userDetailBtn.setVisibility(View.VISIBLE);
                userDetailBtn.setText("添加好友");
                userDeleteBtn.setVisibility(View.GONE);
            }
            userDetailMemberId.setText(userEntity.getMemberid());
            userDetailNickname.setText(userEntity.getNickname());
            userDetailTel.setText(userEntity.getMobile());
            GlideUtils.setRoundImageView(this, userEntity.getAvatar(), R.drawable.default_avatar, userDetailAvatar);
        }
    }

    private void initListener() {
        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
        RxViewHelper.getInstance().clicks(userDetailBtn, aVoid -> doNext());
        RxViewHelper.getInstance().clicks(userDeleteBtn, aVoid -> doShowDialog());
    }

    private void doNext() {
        Intent intent = new Intent(this, SendFriendApplyActivity.class);
        intent.putExtra("UserEntity", userEntity);
        startActivity(intent);
    }

    private void doShowDialog() {
        DialogDeleteFriend dialog = new DialogDeleteFriend(this);
        dialog.show();
        dialog.getOkSubject().subscribe(o -> doDeleteFriend());
    }

    private void doDeleteFriend() {
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .deleteFriend(AppUtils.getSignParams(currentUser.getMemberid(), userEntity.getMemberid()), currentUser.getMemberid(), userEntity.getMemberid())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        UserDao.getInstance().delete(userEntity);
                        try {
                            EMClient.getInstance().chatManager().deleteConversation(userEntity.getMemberid(), true);
                            EMClient.getInstance().contactManager().deleteContact(userEntity.getMemberid());
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                        }
                        setResult(RESULT_OK);
                        finish();
                    }
                });
    }
}
